﻿package com.sff.webservice.utils {
	
	import com.sff.webservice.data.WSOperations;
	import com.sff.webservice.data.WSArgument;
	import com.sff.webservice.data.WSMethod;
	
	public class WSUtils {
		
		public static function createDataVariables(methodName:String, operations:WSOperations, targetNameSpace:String = "http://tempuri.org/", arguments:Array = null): XML {
			
			var wsMethod:WSMethod = operations.getMethodObject(methodName);
			var wsArgument:WSArgument;
			var xmlBody:XML;
			
			// NAMESPACE
			if(operations.getBindingObject(methodName).getInputNamespace() != null) {
				xmlBody = <{"wc:" + methodName} xmlns:wc={operations.getBindingObject(methodName).getInputNamespace()} />
			} else {
				xmlBody = <{methodName} xmlns={targetNameSpace} />
			}
			
			// LIST
			if(arguments.length > 1 || typeof(arguments[0]) != "object") {
				
				for(var i:int = 0; i < wsMethod.arguments.length; i++) {
					wsArgument = wsMethod.arguments[i] as WSArgument;
					
					if(wsArgument.isReference()) {
						xmlBody.appendChild(createReference(operations, wsArgument.name, wsArgument.type, arguments[i]));
					} 
					
					else {
						xmlBody.appendChild(<{wsArgument.name}>{arguments[i]}</{wsArgument.name}>);
					}
				}
			}
			
			// SINGLE OBJECT
			else {		 	
				for(i = 0; i < wsMethod.arguments.length; i++) {
					
					wsArgument = wsMethod.arguments[i];
					
					// EMPTY
					if(wsArgument.isReference()) {	
						// VAN HIER
						xmlBody.appendChild(createReference(operations, wsArgument.name, wsArgument.type, arguments[i]));
						
					}
					
					else if(!arguments[0][wsArgument.name]) {
						xmlBody.appendChild(<{wsArgument.name} />);								
					}
					
					else {						
						xmlBody.appendChild(<{wsArgument.name}>{arguments[0][wsArgument.name]}</{wsArgument.name}>);
					}
				}				
			}
			
			var xml:XML = 	<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/soap/envelope/">
								<soap12:Body>{xmlBody}</soap12:Body>
							</soap12:Envelope>;
			
			//_call.prependChild("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
			
			//trace(xml.toString());
			
			return xml;			
		}
		
		private static function createReference(operations:WSOperations, type:String, name:String, curWSArgument:WSArgument):XML {
			
			var wsMethod:WSMethod = operations.getComplexObject(type);
			var xml:XML = <{name} />;
			
			if(wsMethod.arguments.length > 0) {
				
				var wsArgument:WSArgument; 
				var xmlNode:XML;
				
				
				for(var i:int = 0; i < wsMethod.arguments.length; i++) {
					wsArgument = wsMethod.arguments[i];
					
					if(wsArgument.isReference()) {				
						xml.appendChild(createReference(operations, wsArgument.name, wsArgument.type, curWSArgument[wsArgument.name]));
					} 
					
					else {
						if(wsArgument.name == "anyType") {
							
							xmlNode = <{wsArgument.name} />;
							
							for(var k:String in curWSArgument[i]) {
								xmlNode.appendChild(<{k}>{curWSArgument[i][k]}</{k}>);
							}
							
							xml.appendChild(xmlNode);
						}
						
						else {
							xml.appendChild(<{wsArgument.name}>{curWSArgument[wsArgument.name]}</{wsArgument.name}>);
						}
					}							
				}
			}
			
			else {
				xml.appendChild(curWSArgument);
			}
			
			return xml;
		}
	}
}
